﻿using UnityEngine;
using System.Collections;


public class U3dSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
	protected static T _instance = default(T);
	static readonly object syncLock = new object();

	protected virtual void Awake () {
		if (null == _instance) {
			_instance = gameObject.GetComponent (typeof (T)) as T;
		}
	}

    //protected virtual void OnDestroy () {
    //    _instance = null;
    //}

	void OnApplicationQuit () {
		_instance = null;
		if (null != gameObject) {
			DestroyObject (gameObject);
		}
	}

	public static T Instance {
		get {
			if (null == _instance) {
				_instance = GameObject.FindObjectOfType(typeof (T)) as T;
			}

			return _instance;
		}
	}

    static public bool IsValid()
    {
        return (U3dSingleton<T>._instance != null);
    }
}
